//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
    let curWwwPath = window.document.location.href; // 获取当前网址
    let pathName = window.document.location.pathname; // 获取主机地址之后的目录
    // 项目根目录
    let path = curWwwPath.substring(0, curWwwPath.indexOf(pathName) + pathName.substr(1).indexOf('/') + 1);
    // socket目录
    let webSocketPath = path.replace("http://", "ws://") + "/message/" + customerId;
    let websocket = new WebSocket(webSocketPath);

    // 连接发生错误的回调方法
    websocket.onerror = function () {
        console.log("WebSocket连接发生错误");
    };
    // 连接成功建立的回调方法
    websocket.onopen = function () {
        console.log("WebSocket连接成功");
    };
    // 接收到消息的回调方法
    websocket.onmessage = function (event) {
        let output = document.querySelector("#customerMessage").innerText;
        console.log(output);
        if (isNaN(output)) {
            if (output === "10+")
                output = "10+";
        } else
            output = output === "10"
                ? "10+"
                : output * 1 + 1;
        document.querySelector("#customerMessage").innerHTML = output;
        console.log(event);
        console.log("这是后台推送的消息：" + event.data);
    };
    // 连接关闭的回调方法
    websocket.onclose = function () {
    };
    // 监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        websocket.close();
    };
}